/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.model.powersearch;


import idea.model.qnrlibrary.QuestionTitleSearchView;
import java.awt.BorderLayout;
import java.util.List;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.swing.outline.Outline;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//idea.model.powersearch//QTSearchResults//EN" ,
        autostore = false )
@TopComponent.Description(
        preferredID = "QTSearchResultsTopComponent" ,
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( mode = "editor" , openAtStartup = false )
@ActionID( category = "Window" , id = "idea.model.powersearch.QTSearchResultsTopComponent" )
//@ActionReferences(
//        {
//    @ActionReference( path = "Menu/Window/Search Results" , position = 433 ) ,
//    @ActionReference( path = "Menu/File/Search Results" , position = 433 )
//} )
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_QTSearchResultsAction" ,
        preferredID = "QTSearchResultsTopComponent" )
@Messages(
        {
    "CTL_QTSearchResultsAction=QTSearchResults" ,
    "CTL_QTSearchResultsTopComponent=Search Results Window" ,
    "HINT_QTSearchResultsTopComponent=This is a Search Results window"
} )
public final class QTSearchResultsTopComponent extends TopComponent implements ExplorerManager.Provider
{

    private static ExplorerManager exMgr = new ExplorerManager();


    public QTSearchResultsTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_QTSearchResultsTopComponent() );
        setToolTipText( Bundle.HINT_QTSearchResultsTopComponent() );

        setLayout( new BorderLayout() );

        associateLookup( ExplorerUtils.createLookup( exMgr , getActionMap() ) );

    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        lblQTSearchResults = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        beanTreeView2 = new org.openide.explorer.view.BeanTreeView();

        lblQTSearchResults.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblQTSearchResults, org.openide.util.NbBundle.getMessage(QTSearchResultsTopComponent.class, "QTSearchResultsTopComponent.lblQTSearchResults.text")); // NOI18N

        jScrollPane1.setViewportView(beanTreeView2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblQTSearchResults, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblQTSearchResults)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblQTSearchResults;
    // End of variables declaration//GEN-END:variables


    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }


    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    @Override
    public ExplorerManager getExplorerManager()
    {
        return exMgr;
    }


    public void displayQTSearchResults( List<QuestionTitleSearchView> resultList )
    {
        ExplorerManager exm = getExplorerManager();
        Children kids = Children.create( new QTSearchResultsChildFactory( resultList ) , true );
        Node rootNode = new QTSearchResultsRootNode( kids );
        OutlineView ov = new OutlineView( "Survey: Ref Period" );

        ov.setPropertyColumns(
                "questionnairesTitle" , "Questionnaire Title" ,
                "subTitle" , "Questionnaire Subtitle" ,
                "formDisplayNumber" , "Form Display Number" ,
                "formNumber" , "Form Number" ,
                "category" , "Question Category" ,
                "questionTitle" , "Question Title" ,
                "version" , "Question Version" );

        Outline o = ov.getOutline();
        o.setRootVisible( false );
        o.getColumnModel().getColumn( 0 ).setMaxWidth( 15 );
        //String labelText = lblQTSearchResults.getText();
        int size = resultList.size();
        String res = (size == 1 ? size + " result" : size + " results");
        String labelText = "Power Search Results ";
        //labelText = labelText.replaceAll( "-- [0..9]* result*$", "");
        lblQTSearchResults.setText( labelText + "-- " + res);
        add( lblQTSearchResults , BorderLayout.NORTH );
        add( ov , BorderLayout.CENTER );
        exm.setRootContext( rootNode );

    }

}
